package com.wancaijinfu.widget;


import com.wancaijinfu.actitvity.R;
import com.wancaijinfu.network.MNetWork;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.TextView;

public class LodingDialog extends Dialog {
	private String text;
	private String tag;
	private boolean cancelFlag = true;
	
	public LodingDialog(Context context, int theme, String text) {
		this(context, theme, null, text);
	}

	public LodingDialog(Context context, int theme, String tag, String text) {
		super(context, theme);
		this.tag = tag;
		this.text = text;
		setCancelable(false);
	}
	
	public void setCanCancel(boolean cancel) {
		cancelFlag = cancel;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.toast_loding);
		((TextView)findViewById(R.id.toast_textview)).setText(text);
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (cancelFlag) {
				this.hide();
				MNetWork.getInstance().cancelRequest(tag);
				return false;
			}
		}
		return super.onKeyDown(keyCode, event);
	}
	
	public static Dialog getLodingInstance(Context context, String content) {
		final LodingDialog dialog = new LodingDialog(context, R.style.Lodingdialog, content);
		dialog.setCanCancel(false);
		dialog.show();
		return dialog;
	}
}
